﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;

namespace BitPortal.Admin.Product.Product.Module
{
    public partial class MediaInProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            wAddMedia.AutoLoad.Url = "/Admin/Media/Media/Default.aspx?AddToProduct=" + txtProductID.Text + "&ReloadId=" + GridPanel1.ClientID;
            wAddMedia.LoadContent();
            wAddMedia.Show();
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            int record = 0;
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                if (BitPortal.Business.ProductBLL.DeleteMedia(Convert.ToInt32(txtProductID.Text), Convert.ToInt64(row.RecordID)) > 0)
                {
                    record++;
                }
            }
            Notification.Show(new NotificationConfig
            {
                Title = "Thông báo",
                Icon = Icon.Information,
                Html = "Bỏ " + record + " media thành công!"
            });
            sm.ClearSelections();
            this.Store1.DataSource = ProductBLL.GetMedia(Convert.ToInt32(txtProductID.Text), null, true);
            this.Store1.DataBind();
        }

        public void BindData(long ProductID)
        {
            Models.Product g = Business.ProductBLL.GetByID(ProductID);
            if (g != null)
            {
                Window1.Title = "Ảnh(video) của ' " + g.Name + " '";
                txtProductID.Text = g.ProductID.ToString();
                this.Store1.DataSource = g.GetMedia(null, true);
                this.Store1.DataBind();
                this.Window1.Show();
            }
        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            this.Store1.DataSource = ProductBLL.GetMedia(Convert.ToInt32(txtProductID.Text), null, true);
            this.Store1.DataBind();
        }
    }
}